package main

import (
    "go-os-qemu/pkg/vga"
    "go-os-qemu/pkg/portio"
    "go-os-qemu/pkg/netstack"
)

//go:export irq_timer
func irqTimer() {
    // Heartbeat & scheduler tick
    staticCounter++
    if irqPrintTimer && staticCounter%32 == 0 { // roughly every ~1.7s at ~18.2Hz
        vga.SetColor(vga.ColorLightCyan, vga.ColorBlack)
        vga.Print(".")
        vga.SetColor(vga.ColorLightGray, vga.ColorBlack)
    }
    // Invoke preemptive scheduler time-slice handling
    schedulerTick()
    // Drive net stack periodic tasks: RX poll and ARP expiry
    netstack.Tick()
}

//go:export irq_keyboard
func irqKeyboard() {
    // Read scancode from PS/2 data port and print a simple notice.
    sc := portio.Inb(0x60)
    if irqPrintKeyboard {
        vga.SetColor(vga.ColorYellow, vga.ColorBlack)
        printHexScancode(sc)
        vga.SetColor(vga.ColorLightGray, vga.ColorBlack)
    }
}

var staticCounter uint32
var irqPrintTimer bool
var irqPrintKeyboard bool

func printHexScancode(b byte) {
    const hexdigits = "0123456789ABCDEF"
    vga.Print(" [key:")
    vga.Print(string(hexdigits[b>>4]))
    vga.Print(string(hexdigits[b&0x0F]))
    vga.Print("]")
}